package com.nowcoder.code.hjnc;

public class NC128 {

    public static void main(String[] args) {

    }

    public long maxWater(int[] arr) {

        long res = 0;
        int li = 0;
        for (int i = 1; i < arr.length - 1; i++) {
            if (arr[i] >= arr[li]) {
                li = i;
            } else {
                res = res + arr[li] - arr[i];
            }
        }
        int ri = arr.length - 1;

        if (arr[ri] >= arr[li]) {
            return res;
        }

        for (int i = arr.length - 2; i > li; i--) {
            if (arr[ri] < arr[i]) {
                ri = i;
            }
            res = res - (arr[li] - arr[ri]);
        }

        return res;

    }

}
